package com.qianfeng.web.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianfeng.biz.IOrderBiz;
import com.qianfeng.biz.IOrderDetailBiz;
import com.qianfeng.biz.impl.OrderBizImpl;
import com.qianfeng.biz.impl.OrderDetailBizImpl;
import com.qianfeng.commons.WebUtils;
import com.qianfeng.dto.PageBean;
import com.qianfeng.entity.GoodInfo;
import com.qianfeng.entity.Order;
import com.qianfeng.entity.OrderDetail;
import com.qianfeng.entity.ShopCar;
import com.qianfeng.entity.User;

/**
 * Servlet implementation class OrderServlet
 */
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IOrderBiz orderBiz = new OrderBizImpl();
	private IOrderDetailBiz io=new OrderDetailBizImpl();
	private IOrderDetailBiz odBiz = new OrderDetailBizImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		ShopCar shopCar = ShopCar.getShopCar(request.getSession());
		User user = (User) request.getSession().getAttribute("LOGIN_USER");
		if ("add".equals(action)) {
			add(request, response, shopCar, user);
		}else if("queryOrderById".equals(action)){
			int orderid = WebUtils.StringToInt(request.getParameter("orderid"), -1);
			
			Order order = orderBiz.getOrderById(orderid);
			request.getSession().setAttribute("order", order);
			
			//清空购物车
			response.sendRedirect("ShopCarServlet?action=clear");
		}else if("queryAllOrderList".equals(action)){
			String cp = request.getParameter("currentPage");
			int currPage = Integer.parseInt(cp);
			PageBean page=orderBiz.queryall(currPage);
			request.getSession().setAttribute("page", page);
			List<OrderDetail> orderDetailList = io.queryall();
			request.getSession().setAttribute("orderDetailList", orderDetailList);
			response.sendRedirect("order/orderlist.jsp");
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response, ShopCar shopCar, User user)
			throws IOException {
		String phone = request.getParameter("phone");
		String shouhuoren = request.getParameter("shouhuoren");
		String address = request.getParameter("address");
		String express = request.getParameter("express");
		String pay = request.getParameter("bank");

		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		Random random = new Random();
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 1; i <= 5; i++) {
			sBuffer.append(random.nextInt(10));
		}
		int orderId = WebUtils.StringToInt(year + sBuffer.toString(), -1);
		System.out.println(orderId);
		Order order = new Order(orderId, express, pay, shopCar.getTotalPrice(), user.getId(), shouhuoren, phone,
				address);
		// 添加订单
		orderBiz.addOrder(order);

		// 添加订单明细
		for (GoodInfo good : shopCar.getList()) {
			OrderDetail orderDetail = new OrderDetail(orderId, good.getId(), good.getGoods_name(),
					good.getGoods_price_off(), good.getGoods_description(), good.getCount(), good.getGoods_pic(),
					good.getDanPrice());
			odBiz.addOrderDetail(orderDetail);
		}
		
		//通过订单id查询订单对象
		response.sendRedirect("OrderServlet?action=queryOrderById&orderid="+orderId);
	}

}
